package com.weaver.seconddev.custom.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CallApiUtil {


    public static String RESPONSE_TYPE = "code";

    public static String STATE = "test1234";

    public static String GRANT_TYPE = "authorization_code";
    /**
     * 1、获取code信息
     * @return
     */
    public static String getCode(String HOST ,String CORPID){
        String code = "";
        try {
            String url = HOST + "/papi/openapi/oauth2/authorize?corpid=%s&response_type=%s&state=%s";
            String result = HttpRequest.get(String.format(url,CORPID,RESPONSE_TYPE,STATE)).execute().body();
            JSONObject resultObj = JSON.parseObject(result);
            String errcode = resultObj.getString("errcode");
            if("0".equals(errcode)){
                code = resultObj.getString("code");
            }
        }catch (Exception e){
        
        }
        return code;
    }

    public static String getAccessToken(String HOST,String code,String app_key,String app_secret){
        String accessToken = "";
        try {
            String url = HOST + "/papi/openapi/oauth2/access_token";
            String result = HttpRequest.post(url).form("app_key", app_key).form("app_secret", app_secret).form("grant_type", GRANT_TYPE).form("code", code)
                    .execute().body();
            JSONObject resultObj = JSON.parseObject(result);
            String errcode = resultObj.getString("errcode");
            if("0".equals(errcode)){
                accessToken = resultObj.getString("accessToken");
            }
        }catch (Exception e){}

        return accessToken;
    }
    
    
    public static void main(String[] args) throws Exception {
        String host = "http://10.10.28.31:20600";
        String url = host + "/api/workflow/core/paService/v1/triggerSubRequest";
        Long subsetid = 290004370000000584l ;
        List subWfSetIdslist = new ArrayList();
        subWfSetIdslist.add(subsetid);
        
        
        JSONObject param = new JSONObject();
        param.put("access_token", "428d7cde0f18ae76c9bc9c68ca84c2ee");
        param.put("userIdStr", "1");
        param.put("mainRequestId", "1169512770040700929");
        param.put("subWfSetIds", subWfSetIdslist);
        param.put("dataId","0");
        System.out.println("==param.toString():"+param.toString());
        // 远程调用创建流程。
        String resultStr = HttpRequest.post(url).header("Content-Type", "application/json") .body(param.toString()).execute().body();
        System.out.println("==str:"+resultStr);
        
        
        url = "http://10.10.28.31:10600/papi/openapi/api/workflow/core/paService/v1/triggerSubRequest";
        // 构建JSON格式的请求参数
        String jsonParam = "{" +
                                   "\"mainRequestId\":\"828122634632757249\"," +
                                   "\"access_token\":\"428d7cde0f18ae76c9bc9c68ca84c2ee\"," +
                                   "\"userIdStr\":\"3571957610118121591\"," +
                                   "\"subWfSetIds\":[3571957610118121591]," +
                                   "\"dataId\":\"17913585895869294592\"" +
                                   "}";
        jsonParam="{\"access_token\":\"428d7cde0f18ae76c9bc9c68ca84c2ee\",\"userIdStr\":\"1\",\"dataId\":\"0\",\"mainRequestId\":\"1169512770040700929\",\"subWfSetIds\":[290004370000000584]}";
        
        
        HttpResponse response = HttpRequest.post(url)
                                        .header("Content-Type", "application/json")  // 设置内容类型为JSON
                                        .body(param.toString())  // 设置请求体
                                        .execute();
            
            // 获取响应状态码和响应体
            int statusCode = response.getStatus();
            String responseBody = response.body();
            
            System.out.println("a: " + statusCode);
            System.out.println("b: " + responseBody);
        
        
    }
  
}
